Ext.define('Lms.rms.SystemSummaryPanel',{
	extend : 'Ext.panel.Panel', //Ext.view.View
	initComponent : function(){
		Ext.define('PcModel', {
			extend: 'Ext.data.Model',
			fields: ['id','name','hwState', 'swState', 'ip', 'macAddress', 'hasProjector']
		});
  
		var store = Ext.create('Ext.data.Store', {
			model: 'PcModel',
		    sortInfo: {
		    	field    : 'pcName',
		        direction: 'ASC'
		    },
		    autoLoad : true,
		    proxy : {
		    	url : 'pc/list.do',
		    	type : 'ajax'
		    }
		});
	  
		var contextMenu = Ext.create('Ext.menu.Menu', {
			items: [{
				text : 'PC On',
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/pcOn.do',
			            params: {
			            	ip : selectedRecord.get('ip'),
			            	macAddress : selectedRecord.get('macAddress')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : 'PC Off',
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/pcOff.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : LocaleUtil.getMessage('rms.label.pcRestart'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/pcRestart.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				itemId : 'faultInfoMenu',
				text : LocaleUtil.getMessage('rms.label.faultInfo'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
		    	    Ext.create('Lms.rms.TroubleInfoDialog').openWindow(selectedRecord);
				}
			},{
				text : LocaleUtil.getMessage('rms.label.remoteControl'),
				scope : this,
				handler : this.doInvokeRMS
			},'-',{
				itemId : 'projectorOnMenu',
				text : LocaleUtil.getMessage('rms.label.projectorOn'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/projectorOn.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        }); 
				}
			},{
				itemId : 'projectorOffMenu',
				text : LocaleUtil.getMessage('rms.label.projectorOff'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/projectorOff.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        }); 
				}
			},'-',{
				text : LocaleUtil.getMessage('rms.label.systemPing'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/ping.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : 'Agent Check',
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/agentAlive.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : LocaleUtil.getMessage('rms.label.macAddress'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/getMacAddress.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : LocaleUtil.getMessage('rms.label.versionUpdate'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/softwareVersionUpdate.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : 'Agent Update',
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/agentUpdate.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			},{
				text : LocaleUtil.getMessage('rms.label.restartExefile'),
				scope : this,
				handler : function(){
					var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
					Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
					Ext.Ajax.request({
			            url:'if/softwareRestart.do',
			            params: {
			            	ip : selectedRecord.get('ip')
			            },
			            scope : this,
			            success : function(result, request){
			            	Common.hideWaitBox();
			            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
			            },
			            failure: Common.failureMessage
			        });  
				}
			}]
	    });
	  
	  	this.dataview = Ext.create('Ext.view.View',{
	  		id : 'pcs',
	  		store: store,
	  		tpl  : Ext.create('Ext.XTemplate',
	  			'<tpl for=".">',
	                 '<div class="pc">',
	                  '<img width="60" height="60" src="images/icon/pc{[values.hwState == "OFF"?"":(values.hwState == "OKE"? (values.swState == "OKE"? "-ok" : "-sw-fail"):"-fail")]}.png" />',
	                     '<strong>{name}</strong>',
	                 '</div>',
	             '</tpl>'
	         ),
//	         viewConfig : Common.viewConfig(true),
	         viewConfig : {
	        	 preserveScrollOnRefresh : true
	         },
	         itemSelector: 'div.pc',
	         overItemCls : 'pc-hover',
	         multiSelect : false,
	         autoScroll  : true,
	         listeners: {
	        	 scope : this,
	        	 itemdblclick : function(view, record, item, index, e, eOpts ){
	        		 Ext.create('Lms.rms.SystemInfoDialog').openWindow({
	        			 id : record.get('id'),
	        			 refreshStore : store
	        		 }); 
	        	 },
	        	 itemcontextmenu: function(view, rec, item, index, e) {
	        		 e.stopEvent();//브라우져 context menu 비활성화
	        		 view.getSelectionModel().select([rec]);
	        		 contextMenu.showAt(e.getXY());
	        		 if((rec.get('hwState') == 'OKE' || rec.get('hwState') == 'OFF')  && rec.get('swState') == 'OKE'){
	        			 contextMenu.queryById('faultInfoMenu').hide();
	        		 }else{
	        			 contextMenu.queryById('faultInfoMenu').show();
	        		 }
	        		 if(rec.get('hasProjector') == 'Y'){
	        			 contextMenu.queryById('projectorOnMenu').previousSibling().show();
	        			 contextMenu.queryById('projectorOnMenu').show();
	        			 contextMenu.queryById('projectorOffMenu').show();
	        		 }else{
	        			 contextMenu.queryById('projectorOnMenu').previousSibling().hide();
	        			 contextMenu.queryById('projectorOnMenu').hide();
	        			 contextMenu.queryById('projectorOffMenu').hide();
	        		 }
	        		 return false;
	        	 }
	         }
	  	});
	  
	  	Ext.apply(this, {
	  		id : 'SystemSummaryPanel',
			title : LocaleUtil.getMessage('rms.menu.summary'),
			region : 'center',
			border : true,
			autoScroll  : true,
			items : this.dataview,
			tbar : [{
					xtype : 'combobox',
					store : new Ext.data.JsonStore({
		    			autoLoad:true,
		    			fields: ['id','name'],
		    			proxy : {
		    				url : 'zone/list.do',
		    				type : 'ajax'
		    			},
						listeners : {
							load: function(store){
								var rec = { 'id' : '', 'name' : 'All' };
								store.insert(0,rec);
							}
						}
		    		}),
		    		itemId : 'zone',
		    		fieldLabel: LocaleUtil.getMessage('rms.label.zone'),
		    		labelWidth: 30,
		    		valueField: 'id',
		          	displayField:'name',
		          	forceSelection: true,
		          	editable: false,
		          	emptyText : 'All',
					listeners : {
						scope : this,
						select : function(combo,records,eOpts){
							if(records[0].get('id')){
								store.load({
									params : {
										searchCategory : 'ZONEID',
										searchWordTxt : records[0].get('id')
									}							
								});
							}else{
								store.load();
							}
						}
					}
				},{
					xtype : 'displayfield',
					width : 10
				},{
					xtype : 'displayfield',
					itemId : 'totalCount',
					fieldLabel : LocaleUtil.getMessage('rms.label.normal'),
					width : 70,
					labelWidth: 30
			   	},{
			   		xtype : 'displayfield',
			   		itemId : 'failCount',
			   		fieldLabel : '<font color=red>'+LocaleUtil.getMessage('rms.label.fault')+'</font>',
			   		width : 60,
			   		labelWidth: 30
			   	},'->',{
			   		text : LocaleUtil.getMessage('rms.label.pcAllOn'),
					scope : this,
					handler : function(){
						Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
						Ext.Ajax.request({
				            url:'if/pcAllOn.do',
				            timeout: 120000,
				            scope : this,
				            success : function(result, request){
				            	Common.hideWaitBox();
				            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
				            },
				            failure: Common.failureMessage
				        });  
					}
			   	},{
			   		text : LocaleUtil.getMessage('rms.label.pcAllOff'),
					scope : this,
					handler : function(){
						Common.showWaitBox(LocaleUtil.getMessage('rms.msg.executing'));
						Ext.Ajax.request({
				            url:'if/pcAllOff.do',
				            timeout: 120000,
				            scope : this,
				            success : function(result, request){
				            	Common.hideWaitBox();
				            	Common.alert(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.label.executeResult',[result.responseText]));
				            },
				            failure: Common.failureMessage
				        });
					}
			   	},{
			   		xtyle : 'button',
			   		text : LocaleUtil.getMessage('refresh'),
			   		iconCls: 'x-tbar-loading',
			   		scope : this,
			   		handler : function(){
			   			store.reload();
			   		}
			   	}]
	  	});
	  	this.callParent(arguments);
	  	this.on('show',function(){store.reload();},this);
	  	this.on('activate',function(){
	  		if(!this.interval){
	  			this.interval = setInterval(function(){store.reload();},60000); //1분마다 refresh
	  		}
		},this);
	  	this.on('deactivate',function(){
	  		clearInterval(this.interval);
	  		this.interval = null;
	  	},this);
	  	store.on('load',function(store, records, successful, eOpts){
	  		var failCnt = 0;
	  		for(var i in records){
	  			if((records[i].get('hwState') != 'OKE' && records[i].get('hwState') != 'OFF') || records[i].get('swState') != 'OKE'){
	  				failCnt++;
	  			}
	  		}
	  		this.queryById('totalCount').setValue(records.length - failCnt);
	  		this.queryById('failCount').setValue('<font color=red>'+failCnt+'</font>');
	  	},this);	  	
	  	
	},
	
	doInvokeRMS : function(){
		var selectedRecord = this.dataview.getSelectionModel().getSelection()[0];
		document.RMS_AX.doInvokeRMS(remoteName,selectedRecord.get('ip'),remotePort,remotePass);
	}
});